﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }
        

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Mainform_Load(object sender, EventArgs e)
        {
            var sqlStu = "select * from Articles";
            var dt = DbHelper.GetDataTable(sqlStu);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }

        private void button1_Click(object sender, EventArgs e)//查找
        {
            var Title = textBox1.Text;
            var sql = string.Format("select * from Articles  where  Title like '%{0}%'", Title);
            var dt = DbHelper.GetDataTable(sql );
            dataGridView1.DataSource = dt;

        }

        private void button2_Click(object sender, EventArgs e)//添加
        {
            Editform editform = new Editform();
            var res  =   editform.ShowDialog ();
            if (res == DialogResult.Yes)
            {
                var Title = textBox1.Text;
                var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", Title);
                var dt = DbHelper.GetDataTable(sqlStu);
                dataGridView1.DataSource = dt;

            }
     


        }

        private void button3_Click(object sender, EventArgs e)//更新
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string )dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string )dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime =dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var TGreatedTime = CreatedTime.ToString();



                Editform editform = new Editform(Id ,Title ,Author  , TGreatedTime);
                var res = editform.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var TitleT = textBox1.Text;
                    var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", TitleT);
                    var dt = DbHelper.GetDataTable(sqlStu);
                    dataGridView1.DataSource = dt;
                }
            }
            else 
            {
                MessageBox.Show("你没有先择数据","提示");

            }
         
        }

        private void button4_Click(object sender, EventArgs e)//删除
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["id"].Value;
                var res = MessageBox.Show("你确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete  from Articles where id = {0}", id);
                    var rowCount = DbHelper.AddOrEdiOrDeleteSava(sql);
                    if (rowCount > 0)
                    {
                        var Title = textBox1.Text;
                        var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", Title);
                        var dt = DbHelper.GetDataTable(sqlStu);
                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功！");
                    }
                }
            }
            else
            {
                MessageBox.Show("你没有选择任何数据","提示");
                
            }
        }
    }
}
